package com.econ.powercloud.ems.controller;

import com.alibaba.fastjson.JSON;
import com.econ.powercloud.ems.pojo.requestVO.WebsocketPidRegistryRequestVO;
import com.econ.powercloud.ems.pojo.requestVO.WebsocketSvgRegistryRequestVO;
import com.econ.powercloud.ems.websocket.WebsocketObserverManager;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.stomp.StompHeaderAccessor;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * @author ben on 2019/4/3.
 */
@Controller
public class WebsocketController {
    private static final String DEFAULT_SCENE = "default";

    @Autowired
    private WebsocketObserverManager websocketObserverManager;

    @MessageMapping("/registry/devices")
    public void registryDevices(StompHeaderAccessor accessor, String data) {
        String simpSessionId = accessor.getSessionId();
        List<String> pidList = JSON.parseArray(data,String.class);
        websocketObserverManager.subscribeDevicePid(simpSessionId,DEFAULT_SCENE,Sets.newHashSet(pidList));
    }

    @MessageMapping("/registry/svg")
    public void registrySvg(StompHeaderAccessor accessor, String data){
        String simpSessionId = accessor.getSessionId();
        //String[0] : deviceCode ,String[1] : dit
        List<String[]> list = JSON.parseArray(data,String[].class);
        List<String> pidList = Lists.newArrayList();
        list.stream().forEach(d->{
            pidList.add(d[0] + "." + d[1]);
        });
        websocketObserverManager.subscribeDevicePid(simpSessionId,DEFAULT_SCENE,Sets.newHashSet(pidList));
    }

    /**
     * 最新版的订阅pid接口
     * @param accessor
     * @param requestVO
     */
    @MessageMapping("/registry/scene/devices")
    public void registryDevicePid(StompHeaderAccessor accessor, WebsocketPidRegistryRequestVO requestVO){
        String simpSessionId = accessor.getSessionId();
        websocketObserverManager.subscribeDevicePid(simpSessionId,requestVO.getScene(),Sets.newHashSet(requestVO.getList()));
    }

    /**
     * 最新版的订阅SVG接口
     */
    @MessageMapping("/registry/scene/svg")
    public void registrySvgPid(StompHeaderAccessor accessor, WebsocketSvgRegistryRequestVO requestVO){
        String simpSessionId = accessor.getSessionId();
        List<String> pidList = Lists.newArrayList();
        requestVO.getList().stream().forEach(f->{
            //String[0] : deviceCode ,String[1] : dit
            pidList.add(f[0] + "." + f[1]);
        });
        websocketObserverManager.subscribeDevicePid(simpSessionId,requestVO.getScene(),Sets.newHashSet(pidList));
    }

    @MessageMapping("/clear/scene")
    public void clearScene(StompHeaderAccessor accessor,List<String> sceneList){
        String simpSessionId = accessor.getSessionId();
        websocketObserverManager.clearDataBySessionIdAndScenes(simpSessionId,sceneList);
    }

}
